<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
	<link rel="stylesheet" type="text/css" href="../../css/api.css" />
	<link rel="stylesheet" type="text/css" href="../../css/common.css" />
	<link rel="stylesheet" type="text/css" href="../../css/aui.css" />
  <title>学术</title>
  <style>
		html, body {width: 100%;min-height: 100%;background: #fff;}
    .h20 {height: 0.5rem;background: #f0f0f0;}
    #header_top {
        background: #2997e4;
        padding-top: 25px;
    }
    .fbxq {
      padding: 0 1rem;
      font-size: 0.7rem;
      color: #999;
      margin-bottom: 2rem;
    }
    .fbxq input[type="text"] {
      width: 70%;
      height: 2rem;
      font-size: 0.7rem;
      /*color: #999;*/
      border: none;
      display: inline-block;
    }
    .fbxq h6 {
      color: #999;
      font-size: 0.6rem;
      line-height: 0.8rem;
      margin-top: 0.5rem;
    }
    .upload_outer {
      width: 100%;
    }
    .upload_outer::before,
    .upload_outer::after {
      content: '';
      height:0;
      display: block;
      clear: both;
    }
    .upload_outer > div {
      width: 48%;
      float: left;
      margin-right: 4%
    }
    .upload_outer > div:nth-child(2n) {
      margin-right: 0;
    }
    .fbxq .upload {
      margin-top: 0.5rem;
      position: relative;
      background: #ececec;
      width: 48%;
      padding: 2rem 0;
    }
    .fbxq .upload img {
      width: 32px;
      margin: auto;
    }
    .uploadImg {
      margin-top: 0.5rem;
      position: relative;
      width: 48%;
      padding: 2rem 0;
      background: #ececec;
    }
    .uploadImg img {
      opacity: 0;
        width: 32px;
        margin: auto;
    }
    .uploadImg > span {
      position: absolute;
      top: 3px;
      right: 3px;
      font-size: 0.8rem;
      line-height: 1rem;
      text-align: center;
      width: 1rem;
      height: 1rem;
      z-index: 11;
      background: #ff5757;
      border-radius: 50%;
      -webkit-border-radius: 50%;
    }
    .fbxq input[type="file"] {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      opacity: 0;
      z-index: 1;
    }
    .fbxq_border {
      border-bottom: 1px solid #f0f0f0;
      font-size: 0.7rem;
    }
    .fbxq_border textarea {
      width: 100%;
      background-color: #ececec;
      font-size: 0.7rem;
      border: none;
      padding: 0.5rem;
      margin-top: 0.65rem;
      height: 12rem;
    }
    .fbxq_border span {
      float: right;
    }
    .fbxq_border1 {
      padding: 0.65rem 0;
    }
	</style>
</head>
<body>
  <div id="app">
<header id="header_top" class="aui-bar aui-bar-nav" >

  <a class="aui-pull-left"  onclick="goback()">
      <span class="aui-iconfont aui-icon-left"></span>
  </a>
  <div class="aui-title" id="title">
  发布需求
</div>
    <a class="aui-pull-right" @click="fb">
      发布
    </a>
</header>
<div class="fbxq">
  <div class="fbxq_border">
      需求：<input type="text" placeholder="请输入您的需求,限制为20字符" v-model="title">
  </div>
    <div class="fbxq_border fbxq_border1">
      <!-- 详情：<input type="text" placeholder="请输入需求详情"> -->
      请输入您的详情需求<span>0/300</span>
      <textarea name="name" rows="" cols="" placeholder="请输入需求详情" v-model="xqxq"></textarea>
    </div>
  <h6>添加图片</h6>
  <div class="upload_outer">
    <div class="upload" @click="showAction()">
      <img src="../../image/jr3@3x.png" alt="">
      <!-- <input type="file" class="file"> -->
    </div>
    <div class="uploadImg" v-for="(item, index) in imageList" :key="index" :style="'background:url('+item+') no-repeat center/cover'" v-if="item != ''">
      <img src="../../image/jr3@3x.png" alt="">
      <span class="clearImg" @click="clearImg(index)">×</span>
    </div>
  </div>
</div>
</div>
</body>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/zepto.js"></script>
<script type="text/javascript" src="../../script/vue.min.js"></script>
<script type="text/javascript" src="../../script/utils.js"></script>
<script type="text/javascript" src="../../script/common.js"></script>
<script type="text/javascript">
function goback () {
  api.closeWin({name:api.pageParam.name});
}
// 完成首页初始化
apiready = function(){
//  var FNImageClip = api.require('FNImageClip');



	var app = new Vue({
		el: '#app',
		data: {
      title: '',
      xqxq: '',
      imageList: [],
      imageStr: '',
      token:''
		},
		created: function() {


      var $_this = this;

      $_this.token = $api.getStorage('token') //token

      if(!$api.getStorage('token')){
        api.confirm( {
            title: '是否登录',
            msg: '确认是否登录？',
            buttons: [ '确定', '取消' ]
          }, function ( ret, err ) {
            var index = ret.buttonIndex;
            if ( index == 1 ) {
              api.openWin({
                name: 'login',
                url: '../five_frame/login_header.html',
                delay: 200,
                bounces: false
              });
              return;
            } else {
              api.closeWin({
                  name: name
              });
              return
            }
          })
          return
        }

		},

		methods: {

      fb: function () {
        var $_this = this;


        if ($_this.title == '') {
          //alert('请输入活动标题')
          api.toast({ msg: '请输入您的需求',duration: 2000,location:'middle' });
          return
        } else if ($_this.xqxq == '') {
          //alert('请输入活动开始时间')
          api.toast({ msg: '请输入您的需求详情',duration: 2000,location:'middle' });
          return
        }
        if($_this.imageList.length <=0){
          api.toast({
              msg: '请上传图片',
              duration: 2000,
              location:'middle'
          });
          return;
        }
        for(var i=0;i<$_this.imageList.length;i++){
          $_this.imageStr += ($_this.imageList[i] + ';')
        }
        $_this.imageStr = $_this.imageStr.slice(0, -1)

  			api.ajax({
  				url: window.Url.applyScience,
          method: 'post',
          headers: {
			        'Content-Type': 'application/json',
                  'baseParams': $api.getStorage('token') //token
          },
          data: {
            body: {
              title: $_this.title, //活动标题
              content: $_this.xqxq, //活动详情
              image: $_this.imageStr,
              equipent: api.deviceId,
              science_statu: 1
            }
          },
  				timeout: 300,
  			}, function(ret, err) {
  					if (ret) {
              if (ret.code == 2) {
                  alert('用户未登录，请返回登录')
                  $api.rmStorage('token')
                  $api.rmStorage('data')
                  goback()
                   return
                }
              // api.alert({ msg: JSON.stringify(ret) });
              if (ret.code == 0) {
                api.toast({ msg: '发布成功',duration: 2000,location:'middle' });
                goback()
              } else {
                  api.toast({ msg: ret.message,duration: 2000,location:'middle' });
              }
  					} else {
  							api.alert({ msg: JSON.stringify(err) });
  					}
  			})
      },
      // 上传图片
      showAction: function (){
        var $_this = this
        api.actionSheet({
            title: '活动图片上传',
            cancelTitle: '取消',
            buttons: ['拍照','从手机相册选择']
        }, function(ret, err) {
            if (ret) {
                $_this.getPicture(ret.buttonIndex);
            }
        });
      },
      clearImg: function (index) {
        // this.imageList[index] = ''
        this.$set(this.imageList, index, '')
      },
      getPicture: function (sourceType) {
        var $_this = this
              if(sourceType==1){ // 拍照
                  api.getPicture({
                      sourceType: 'camera',
                      encodingType: 'jpg',
                      mediaValue: 'pic',
                      allowEdit: false,
                      destinationType: 'base64',
                      quality: 90,
                      saveToPhotoAlbum: true
                  }, function(ret, err) {
                      if (ret) {

                        var formData=ret.data;
                        var formData=ret.base64Data;
                        api.ajax({
                            method:"post",
                            url:window.Url.addPicture,
                               data: {
                                 files: {file: ret.data}
                               }

                        },function(ret,err){
                            if(ret){
                              if(ret.error == '0')
                              {
                                  $_this.imageList.push(ret.url)
                              }
                              else{
                                  api.toast({ msg: ret.message,duration: 2000,location:'middle' });
                              }
                              console.log("图片返回"+JSON.stringify(ret))
                            }else{
                                api.toast({
                                  msg : '你没有选择图片',
                                  location : 'middle'
                                })
                            }
                        })
                      }else {
                          alert(JSON.stringify(err));
                      }
                  });
              }
              else if(sourceType==2){ // 从相机中选择
                  api.getPicture({
                          sourceType: 'album',
                          encodingType: 'png',
                          mediaValue: 'pic',
                          destinationType: 'url',
                          allowEdit:true,
                          quality: 50,
                          saveToPhotoAlbum:false,
                          targetWidth: 750,
                          targetHeight: 750
                      }, function(ret, err) {
                          if (ret) {
                              var formData=ret.data;
                              // $('.img').prop('src',ret.data);
                              var formData=ret.base64Data;
                              api.ajax({
                                  method:"post",
                                  url:window.Url.addPicture,
                                     data: {
                                       files: {file: ret.data}
                                     }

                              },function(ret,err){
                                  if(ret){
                                    if(ret.error == '0')
                                    {
                                      // $(".imglist .aui-row-padded").append("<div class='aui-col-xs-3'><img src='"+ret.url+"' /></div>");
                                        $_this.imageList.push(ret.url)
                                    }
                                    else{
                                        api.toast({ msg: ret.message,duration: 2000,location:'middle' });
                                    }
                                    console.log("图片返回"+JSON.stringify(ret))
                                  }else{
                                      api.toast({
                            						msg : '你没有选择图片',
                            						location : 'middle'
                            					})
                                  }
                              })
                          } else {
                              api.alert({msg:JSON.stringify(err)});
                          }
                  });
              }
          }
		}
	})
}
</script>
</html>
